package org.foxas.ui
{
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import org.foxas.base.FGeom;
	import org.foxas.classarray.TextFieldArray;

	public class FTextList extends Sprite
	{
		public var frameExsitMax:int = 150;
		protected var txtValues:TextFieldArray = new TextFieldArray();
		protected var txtLabels:TextFieldArray = new TextFieldArray();
		public var spaceHeight:int = 4;
		public var spaceBorder:int = 8;
		public var widthMax:int = 200;
		public var textFormatValue:TextFormat = new TextFormat("Times",12,0x0099FF,true);
		public var textFormatLabel:TextFormat = new TextFormat("Times",12,0xFF9900,true);
		public var maxLabelWidth:int = 0;
		public var maxValueWidth:int = 0;
		protected var maxEnabledValue:int=-1;
		protected var maxEnabledLabel:int=-1;
		public function FTextList()
		{
			super();
		}
		public function build(_x:int,_y:int,_valueArray:Array,_labelArray:Array=null):void{
			this.graphics.clear();
			var i:int;
			this.maxLabelWidth = 0;
			this.maxValueWidth = 0;
			if(_labelArray){
				this.maxEnabledLabel = _labelArray.length-1;
			}else{
				this.maxEnabledLabel = -1;
			}
			for(i=0;i<this.txtLabels.length;i++){
				if(i<=this.maxEnabledLabel){
					this.fitTxtLabel(i,this.txtLabels.c(i),_labelArray[i]);
					this.txtLabels.c(i).visible = true;
				}else{
					this.txtLabels.c(i).visible = false;
				}
			}
			for(i=this.txtLabels.length;i<=this.maxEnabledLabel;i++){
				txt = new TextField();
				this.txtLabels.push(txt);
				txt.selectable = false;
				txt.autoSize = TextFieldAutoSize.LEFT;
//				txt.border = true;
				txt.x = this.spaceBorder;
				this.addChild(txt);
				this.fitTxtLabel(i,txt,_labelArray[i]);
			}
			this.maxEnabledValue = _valueArray.length - 1;
			for(i=0;i<this.txtValues.length;i++){
				if(i<=this.maxEnabledValue){
//					this.txtValues.c(i).text = _valueArray[i];
					this.fitTxtValue(i,this.txtValues.c(i),_valueArray[i]);
					this.txtValues.c(i).visible = true;
				}else{
//					this.txtValues.c(i).text = "";
					this.txtValues.c(i).visible = false;
				}
			}
			var txt:TextField;
			for(i=this.txtValues.length;i<=this.maxEnabledValue;i++){
				txt = new TextField();
				this.txtValues.push(txt);
				txt.selectable = false;
				txt.autoSize = TextFieldAutoSize.LEFT;
				//txt.wordWrap = true;
//				txt.border = true;
//				txt.width = 200;
				this.addChild(txt);
				this.fitTxtValue(i,txt,_valueArray[i]);
//				txt.text = _valueArray[i];
			}
			layoutRefur();
			FGeom.DrawRect(this,0,0,this.width,this.height,0xD9FFFF,0.7,0x6699FF,0.7,3);
		}
		public override function get width():Number{
			if(this.maxEnabledValue==-1){
				return super.width;
			}
			return maxValueWidth + this.txtValues.c(this.maxEnabledValue).x + this.spaceBorder;
		}
		public override function get height():Number{
			if(this.maxEnabledValue==-1){
				return super.height;
			}
			return this.txtValues.c(this.maxEnabledValue).height + this.txtValues.c(this.maxEnabledValue).y + this.spaceBorder;
		}
		protected function layoutRefur():void{
			var i:int;
			var txt:TextField;
//				for(i=0;i<=this.maxEnabledValue;i++){
//					txt = this.txtValues.c(i);
//					if(i==0){
//						txt.y = this.spaceBorder;
////					if(this.maxEnabledLabel==0){
////						txt.x = this.spaceBorder;
////					}else{
//						txt.x = this.spaceBorder + this.maxLabelWidth;
////					}
//					}else{
//						txt.y = txtValues.c(i-1).y+txtValues.c(i-1).height+spaceHeight;
//						txt.x = txtValues.c(i-1).x;
//					}
//				}
				for(i=0;i<=this.maxEnabledLabel;i++){
					txt = this.txtLabels.c(i);
					txt.y = this.txtValues.c(i).y;
				}
		}
		protected function fitTxtLabel(i:int,txt:TextField,_text:String):void{
			txt.defaultTextFormat = this.textFormatLabel;
			txt.text = _text;
			this.maxLabelWidth = Math.max(this.maxLabelWidth,txt.width);
		}
		protected function fitTxtValue(i:int,txt:TextField,_text:String):void{
			txt.defaultTextFormat = this.textFormatValue;
//			txt.width=10;
			txt.wordWrap = false;
			txt.text = _text;
//			trace(txt.width);
			
			if(txt.width>this.widthMax){
				txt.wordWrap = true;
				txt.width=this.widthMax;
			}
			this.maxValueWidth = Math.max(this.maxValueWidth,txt.width);
			txt = this.txtValues.c(i);
			if(i==0){
				txt.y = this.spaceBorder;
			if(this.maxEnabledLabel==-1){
				txt.x = this.spaceBorder;
			}else{
				txt.x = this.spaceBorder*2 + this.maxLabelWidth;
			}
			}else{
				txt.y = txtValues.c(i-1).y+txtValues.c(i-1).height+spaceHeight;
				txt.x = txtValues.c(i-1).x;
			}
		}
	}
}